// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import FastClick from 'fastclick'
import VueRouter from 'vue-router'
import App from './App'
import router from './router'
import VueResource from 'vue-resource'
import  { ToastPlugin,AlertPlugin,LoadingPlugin,ConfirmPlugin } from 'vux'
import common from '../wechat/static/common/common'
import API from '../wechat/static/constant/urlconstant'
require("./components/common/common.css");
import VueScroller from 'vue-scroller'
import VueClipboard from 'vue-clipboard2'

Vue.use(VueClipboard)
Vue.use(VueScroller)
Vue.use(ToastPlugin)
Vue.use(AlertPlugin)
Vue.use(LoadingPlugin)
Vue.use(ConfirmPlugin)
Vue.use(VueResource);
Vue.use(require('vue-wechat-title'))
FastClick.attach(document.body)
Vue.config.productionTip = false
console.log(API)
function intercept() {
  router.beforeEach((to, from, next) => {
    if (!common.getCookie("openId")) {
      let webUrl = window.location.href;
      if (webUrl.indexOf("code=") > 0) {
        let code = webUrl.split("code=")[1].split("&")[0];
        let url = API.ACCOUNT.SAVE,
          param = {
            code: code
          };
        Vue.http.post(url, param, { emulateJSON: true }).then(
          res => {
            let data = res.data;
            if (data.status == 200) {
              console.log(data,'openId')
              let id = data.bussData.bussData.openId;
              common.setCookie("openId", id)
              next();
            } else {
              console.log(data);
              console.log(data.msg);
            }
          },
          res => {
            console.log(res);
          }
        );
        return;
      }
      let test =API.DOMAIN.DOMAIN;
      let online = window.location.href.includes("code")
          ? window.location.href.split("?")[0]
          : window.location.href,
        REDIRECT_URI = window.location.host == "localhost:8080" ? test :online,
        APPID = "wx0977fadc1ba41ee9",
        SCOPE = "snsapi_userinfo",
        STATE = "";
      location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APPID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=${SCOPE}#wechat_redirect`;
    } else {
      next();
    }
  });
}



Vue.http.interceptors.push(function(request, next) {
  //拦截器
  next(res => {
    //401 登陆过期跳转
    if (res.data.status == 401) {
      common.setCookie("sessionId", "");
      router.replace('/login')
    }
  });
});

//  intercept()

/* eslint-disable no-new */
new Vue({
  router,
  render: h => h(App)
}).$mount('#app-box')



